package cn.stylefeng.guns.sys.modular.metadata.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.metadata.entity.Metadata;
import cn.stylefeng.guns.sys.modular.metadata.mapper.MetadataMapper;
import cn.stylefeng.guns.sys.modular.metadata.model.params.MetadataParam;
import cn.stylefeng.guns.sys.modular.metadata.model.result.MetadataResult;
import cn.stylefeng.guns.sys.modular.metadata.service.MetadataService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 元数据管理表 服务实现类
 * </p>
 *
 * @author author
 * @since 2020-11-17
 */
@Service
public class MetadataServiceImpl extends ServiceImpl<MetadataMapper, Metadata> implements MetadataService {

    @Override
    public void add(MetadataParam param) {
        Metadata entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MetadataParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(MetadataParam param) {
        Metadata oldEntity = getOldEntity(param);
        Metadata newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MetadataResult findBySpec(MetadataParam param) {
        return null;
    }

    @Override
    public List<MetadataResult> findListBySpec(MetadataParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(MetadataParam param) {
        param.setIsDelete(1);
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public int deleteMetadataInfo(Integer id) {
        return this.baseMapper.deleteMetadata(id);
    }

    @Override
    public JSONObject queryMetadataDetailInfo(MetadataParam param, Integer page, Integer limit) {
        JSONObject obj = new JSONObject();
        Integer beforeLimit = null;
        Integer afterLimit = null;
        if (page != null && limit != null) {
            beforeLimit = (page - 1) * limit;
            afterLimit = limit;
        }
        List<Map<String, Object>> list = this.baseMapper.queryMetadataDetail(param, beforeLimit, afterLimit);
        List<Map<String, Object>> listCount = this.baseMapper.queryMetadataDetail(param, null, null);
        obj.put("data", list);
        obj.put("code", 0);
        obj.put("count", listCount.size());
        obj.put("msg", "请求成功");
        return obj;
    }

    @Override
    public int creatTable(MetadataParam param) {
        List<String> list = this.baseMapper.queryCreatTable(param);
        String sql = "";
        for (int i = 0; i < list.size(); i++) {
            sql = sql + list.get(i);
        }
        //执行建表sql
        int x = 0;
        try {
            this.baseMapper.createTable(sql);
            x = 1;
        } catch (Exception e) {
            x = 3;
        }
        return x;
    }

    @Override
    public List<Map<String, Object>> queryMetadataInfo(MetadataParam param) {
        return this.baseMapper.queryMetadataDetail(param, null, null);
    }

    private Serializable getKey(MetadataParam param) {
        return param.getMetadataId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Metadata getOldEntity(MetadataParam param) {
        return this.getById(getKey(param));
    }

    private Metadata getEntity(MetadataParam param) {
        Metadata entity = new Metadata();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
